package cn.bone.base.conf;

import cn.bone.base.interceptor.AuthInterceptor;
import cn.bone.base.interceptor.LoggerInterceptor;
import cn.bone.base.interceptor.MyInterceptor;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Bean
    public LoggerInterceptor loggerInterceptor(){
        return new LoggerInterceptor();
    }

    @Bean
    public AuthInterceptor authInterceptor(){
        return new AuthInterceptor();
    }

    @Bean
    public MyInterceptor myInterceptor(){
        return new MyInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        /**日志记录,优先级0*/

        //拦截的url
        List<String> logPatterns = new ArrayList<>();
        logPatterns.add("/ou/**");
        logPatterns.add("/my/**");
        //过滤的url
        List<String> logExcludePatterns = new ArrayList<>();
        registry.addInterceptor(loggerInterceptor())
                .order(0)
                .addPathPatterns(logPatterns);


        /**token校验,优先级1*/
        List<String> authPatterns = new ArrayList<>();
        authPatterns.add("/ou/**");
        List<String> authExcludePatterns = new ArrayList<>();
        authExcludePatterns.add("/ou/mng/login");

        //excel导入房屋信息
        //authExcludePatterns.add("/ou/house/excelImportTemplate");
        registry.addInterceptor(authInterceptor())
                .order(1)
                .addPathPatterns(authPatterns)
                .excludePathPatterns(authExcludePatterns);

        /**后台校验*/
        List<String> myPatterns = new ArrayList<>();
        myPatterns.add("/my/*");
        List<String> myExcludePatterns = new ArrayList<>();
        myExcludePatterns.add("/my/login");
        registry.addInterceptor(myInterceptor())
                .order(2)
                .addPathPatterns(myPatterns)
                .excludePathPatterns(myExcludePatterns);


    }



    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        //1.设置序列化规则
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty
        );
        //2.将null处理为空字符串
        fastJsonConfig.setSerializeFilters(new ValueFilter() {
            @Override
            public Object process(Object o, String s, Object o1) {
                //o 是class
                //s 是key值
                //o1 是value值
                if (null == o1){
                    o1 = "";
                }
                return o1;
            }
        });
        converter.setFastJsonConfig(fastJsonConfig);
        //3.处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        converter.setSupportedMediaTypes(fastMediaTypes);
        converters.add(converter);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/**")
                .addResourceLocations("classpath:/static/assets/");
    }
}
